package dwz.business.article.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wx.util.CodeUtils;
import dwz.business.article.Article;
import dwz.business.article.ArticleServiceMgr;
import dwz.business.article.ArticleStatus;
import dwz.framework.sys.business.AbstractBusinessObjectServiceMgr;
import dwz.framework.sys.exception.ServiceException;
import dwz.persistence.BaseConditionVO;
import dwz.persistence.beans.WechatArticle;
import dwz.persistence.mapper.WechatArticleMapper;


@Transactional(rollbackFor = Exception.class)
@Service(ArticleServiceMgr.SERVICE_NAME)
public class ArticleServiceMgrImpl extends AbstractBusinessObjectServiceMgr implements ArticleServiceMgr {

	@Autowired
	private WechatArticleMapper articleMapper;
	
	@Override
	public List<Article> searchArticle(BaseConditionVO vo) {
		List<Article> bos = new ArrayList<Article>();
		RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		List<WechatArticle> pos = articleMapper.findPageBreakByCondition(vo, rb);
		for (WechatArticle po : pos) {
			bos.add(new Article(po));
		}
		return bos;
	}

	@Override
	public Integer searchArticleNum(BaseConditionVO vo) {
		Integer count = articleMapper.findNumberByCondition(vo);
		return count;
	}

	@Override
	public Article getArticle(int id) {
		WechatArticle po = articleMapper.load(id);
		if (po == null) return null;
		return new Article(po);
	}

	@Override
	public void updateArticle(Article article) {
		// TODO Auto-generated method stub
		Date now = new Date();
		WechatArticle po = article.getWechatArticle();
		po.setUpdateTime(now);
		
		articleMapper.updateSelective(po);
	}

	@Override
	public void addArticle(Article article) throws ServiceException {
		// TODO Auto-generated method stub
		Date now = new Date();
		WechatArticle po = article.getWechatArticle();
		if(po!=null){
			po.setCreateTime(now);
			po.setUpdateTime(now);
			po.setStatus(ArticleStatus.ACTIVE.toString());
			po.setCode(CodeUtils.getCode());
			articleMapper.insert(po);
		}
	}

	@Override
	public void updateStatus(int id, String status) {
		articleMapper.updateStatus(id, status, new Date());
		
	}

}
